<template>
  <div>
    <el-dialog :title="info.isadd?'商品分类添加':'商品分类修改'" :visible.sync="info.isshow">
      {{form}}
      <el-form :model="form">
        <el-form-item label="上级分类" label-width="120px">
          <el-select placeholder="请选择" v-model="form.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <el-upload class="avatar-uploader" action="#" :show-file-list="false" :on-change="upload">
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qx">取 消</el-button>
        <el-button type="primary" v-if="info.isadd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { reqcateadd, reqcateinfo, reqcateedit } from "../../../request/api";
import { errorAlert, successAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      imgUrl: "",
      form: {
        pid: "",
        catename: "",
        img: null,
        status: "2",
      },
    };
  },
  computed: {
    ...mapGetters({
      list: "cate/list",
    }),
  },
  methods: {
    ...mapActions({
      reqlist: "cate/reqlist",
    }),
    //清空
    emity() {
      this.form = {
        pid:"",
        catename: "",
        img: null,
        status: "2",
      };
      this.imgUrl = "";
    },
    qx() {
      this.info.isshow = false;
    },
    //上传图片
    upload(e) {
      let file = e.raw;
      // console.log(file);
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }
      let imgArr = [".jpg", ".jpeg", ".png", ".gif"];
      let extname = path.extname(file.name);
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.pid=="") {
          errorAlert("上级分类不能为空");
          return;
        }
        if (this.form.catename == "") {
          errorAlert("分类名称不能为空");
          return;
        }
        if (!this.form.img) {
          errorAlert("图片不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
        reqcateadd(this.form).then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            successAlert(res.data.msg);
            this.emity();
            this.qx();
            this.reqlist();
          }
        });
    },
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;

          this.imgUrl = this.$pre + this.form.img;
        }
      });
    },
    update() {
         reqcateedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.qx();
          this.emity();
          this.reqlist();
        }
      })
    },
  },
};
</script>

<style scoped>
.avatar-uploader /deep/.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>